define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/certification/profile/index' + location.search,
                    add_url: 'user/certification/profile/add',
                    edit_url: 'user/certification/profile/edit',
                    del_url: 'user/certification/profile/del',
                    multi_url: 'user/certification/profile/multi',
                    import_url: 'user/certification/profile/import',
                    table: 'user_profile',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'real_name', title: __('Real_name'), operate: 'LIKE'},
                        {field: 'idcard_no', title: __('Idcard_no'), operate: 'LIKE'},
                        {
                            field: 'idcard_front', title: __('Idcard_front'), operate: false,
                            events: Table.api.events.image,
                            formatter: function (value, row, index) {
                                if (value) {
                                    return Table.api.formatter.image.call(this, value, row, index)
                                }
                            }
                        },
                        {
                            field: 'idcard_back', title: __('Idcard_back'), operate: false,
                            events: Table.api.events.image,
                            formatter: function (value, row, index) {
                                if (value) {
                                    return Table.api.formatter.image.call(this, value, row, index)
                                }
                            }
                        },
                        {
                            field: 'idcard_hold', title: __('Idcard_hold'), operate: false,
                            events: Table.api.events.image,
                            formatter: function (value, row, index) {
                                if (value) {
                                    return Table.api.formatter.image.call(this, value, row, index)
                                }
                            }
                        },
                        {
                            field: 'certification_status',
                            title: __('Certification_status'),
                            searchList: {0: '未认证', 1: '待审核', 2: '已认证'},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'verify',
                                    text: '审核',
                                    title: '审核确认信息',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    visible: function (row) {
                                        return row.certification_status === 1
                                    },
                                    click: function (btn, row) {
                                        var verify = function (status, message) {
                                            status = status ? 'allow' : 'refuse'
                                            Fast.api.ajax({
                                                url: 'user/certification/profile/verify?ids=' + row.user_id,
                                                data: {status, message}
                                            }, function (res) {
                                                table.bootstrapTable('refresh')
                                                Layer.closeAll()
                                            }, function (err) {
                                                Layer.closeAll()
                                            })
                                        }
                                        Layer.prompt({
                                            title: '审核',
                                            formType: 2,
                                            value: '审核通过',
                                            btn: ['通过', '拒绝'],
                                            btn2: function (index, lay) {
                                                // 审核拒绝
                                                let msg = lay.find('.layui-layer-input').val()
                                                if (msg === '审核通过') {
                                                    newMsg = '审核未通过'
                                                    lay.find('.layui-layer-input').val(newMsg)
                                                    Layer.confirm('拒绝信息：' + newMsg, {
                                                        title: '拒绝提示'
                                                    }, function () {
                                                        verify(false, newMsg)
                                                    }, function () {
                                                        lay.find('.layui-layer-input').val(msg)
                                                    })
                                                    return false
                                                }
                                                verify(false, msg)
                                                return false
                                            }
                                        }, function (msg) {
                                            // 审核通过
                                            if (msg !== '审核通过') {
                                                Layer.confirm('通过信息：' + msg, {
                                                    title: '通过提示'
                                                }, function () {
                                                    verify(true, msg)
                                                })
                                                return false
                                            }
                                            verify(true, msg)
                                        })
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});